<?php
    require_once '../config/config.php';
    require_once '../inc/db.php';

    

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['radius'])) {
        $radius = $_POST['radius'];
        // Turin location: 45.077157,7.687629
        
        if(is_numeric($radius)) {
            $lat = "45.077157";
            $long = "7.687629";
            $api = "http://www.geoplugin.net/extras/nearby.gp?lat=".$lat."&long=".$long."&format=json&radius=".$radius;
            $locations = json_decode(file_get_contents($api),true);

            // Save to database
            // TRUNCATE table first
            $stmt = $mysqli->prepare("TRUNCATE newsletter_nearest");
            if ($stmt) {
                $stmt->execute();
                $stmt->close();

                $successCount = 0;
                foreach ($locations as $loc) {
                     $query = "INSERT INTO newsletter_nearest (place, region, latitude, longitude) VALUES(?,?,?,?)";
                    $stmt = $mysqli->prepare($query);
                    if ($stmt) {
                        $stmt->bind_param("ssss",$loc['geoplugin_place'], $loc['geoplugin_region'], $loc['geoplugin_latitude'], $loc['geoplugin_longitude']);
                        if($stmt->execute()) $successCount++;
                    }
                }
                if($successCount == count($locations)) {
                    $success = "All place saved";
                    
                }
            }
            
           
        }else{
            $error = "Please enter a valid number";
        }
    }
    // Get saved place
    $stmt = $mysqli->prepare("SELECT place, region from newsletter_nearest");
    if ($stmt) {
        if($stmt->execute()){
            $stmt->bind_result($place, $region);
            $i = 0;
            while ($stmt->fetch()) {
                $places[$i]['place'] = $place;
                $places[$i]['region'] = $region;
                $i++;
            }
            $stmt->close();
        }
    }
 ?>
<?php $page = 'nearby'; ?>
<?php include 'inc/header.php'; ?>
            <br>
            <br>
            <br>
            <div class="col-md-6 col-md-offset-3">
    			<div class="panel panel-primary">
    			  	<div class="panel-heading">
    			    	<h3 class="panel-title">Nearby of Turin</h3>
    			  	</div>
    			  	<div class="panel-body">
    			    	<form action="nearby.php" role="form" method="post">
                            <div class="form-group">
                                <label for="radius">Radius (miles)</label>
                                <p class="success"><?php if(isset($success)) echo $success; ?></p>
                                <p class="error"><?php if(isset($error)) echo $error; ?></p>
                                <input type="text" class="form-control" name="radius" placeholder="Enter radius to get nearby place">
                                <br>
                                <input type="submit" value="Get Locations" name="get-nearby" class="btn btn-primary">
                            </div>            
                        </form>
    			  	</div>
    			</div>
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title">Saved place nearby Turin</h3>
                    </div>
                    <table class="table">
                        <thead>
                            <tr>
                                <td><strong>Place</strong></td>
                                <td><strong>Region</strong></td>
                            </tr>
                        </thead>
                         <?php if(isset($places)) { ?>
                            <?php foreach ($places as $nearby) { ?>
                                <tr>
                                    <td><?php echo $nearby['place']; ?></td>
                                    <td><?php echo $nearby['region']; ?></td>
                                </tr>
                            <?php }//end foreach
                            }//end if ?>
                        </ul>
                    </table>
                </div>
            </div>
        </div>

        <!-- Script -->
        <script src="js/jquery.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>
<?php
    if (isset($mysqli)) {
        mysqli_close($mysqli);
    }
?>
<!-- 45.077157,7.687629 -->